package org.thunms.framework.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Set;

import org.apache.commons.io.Charsets;


public class FileUtils extends org.apache.commons.io.FileUtils implements Serializable {
	private static final long serialVersionUID = 7215681802715490460L;

	/**
     * Reads the contents of a file line by line to a List of Strings.
     * The file is always closed.
     *
     * @param file  the file to read, must not be {@code null}
     * @param encoding  the encoding to use, {@code null} means platform default
     * @return the list of Strings representing each line in the file, never {@code null}
     * @throws IOException in case of an I/O error
     * @since 2.3
     */
    public static Set<String> readLinesSet(File file, Charset encoding) throws IOException {
        InputStream in = null;
        try {
            in = openInputStream(file);
            return IOUtils.readLinesSet(in, Charsets.toCharset(encoding));
        } finally {
        	org.apache.commons.io.IOUtils.closeQuietly(in);
        }
    }

    /**
     * Reads the contents of a file line by line to a List of Strings. The file is always closed.
     * 
     * @param file
     *            the file to read, must not be {@code null}
     * @param encoding
     *            the encoding to use, {@code null} means platform default
     * @return the list of Strings representing each line in the file, never {@code null}
     * @throws IOException
     *             in case of an I/O error
     * @throws UnsupportedCharsetException
     *             thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
     *             supported.
     * @since 1.1
     */
    public static Set<String> readLinesSet(File file, String encoding) throws IOException {
        return readLinesSet(file, Charsets.toCharset(encoding));
    }

    /**
     * Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
     * The file is always closed.
     *
     * @param file  the file to read, must not be {@code null}
     * @return the list of Strings representing each line in the file, never {@code null}
     * @throws IOException in case of an I/O error
     * @since 1.3
     */
    public static Set<String> readLinesSet(File file) throws IOException {
        return readLinesSet(file, Charset.defaultCharset());
    }
    
    
    
   
    public static void readLines(File file, Charset encoding,LineRead read) throws IOException {
        InputStream in = null;
        try {
            in = org.apache.commons.io.FileUtils.openInputStream(file);
            IOUtils.readLines(in, Charsets.toCharset(encoding),read);
        } finally {
        	org.apache.commons.io.IOUtils.closeQuietly(in);
        }
    }

   
    public static void readLines(File file, String encoding,LineRead read) throws IOException {
        readLines(file, Charsets.toCharset(encoding),read);
    }

  
    public static void readLines(File file,LineRead read) throws IOException {
       readLines(file, Charset.defaultCharset(),read);
    }
}
